Whats App Client Integration
This document explains the WhatsApp Web client integration architecture for the bulk messaging system. It covers the Client initialization pattern using LocalAuth strategy, Puppeteer browser configuration, event-driven architecture (qr, ready, authenticated, auth_failure, disconnected), QR code generation and display via the QRCode library, message sending workflow with personalization and rate limiting, contact import functionality for CSV and TXT formats, authentication lifecycle management, session persistence and cleanup procedures, and common integration issues with troubleshooting strategies.
The integration spans three primary areas:
Electron main process orchestrating WhatsApp Web client lifecycle and IPC
React renderer components managing UI state and user interactions
Python backend utilities for advanced contact processing and validation
WhatsApp Client, Events, IPC"] PRE["preload.js
IPC Bridge"] end subgraph "React Renderer" WAF["WhatsAppForm.jsx
UI & State"] PYO["pyodide.js
Manual Numbers Parser"] end subgraph "Python Backend" APP["app.py
Flask API"] EXC["extract_contacts.py
CLI Utilities"] REQ["requirements.txt
Dependencies"] end subgraph "External Libraries" WWEB["whatsapp-web.js
Client & LocalAuth"] QRCODE["qrcode
QR Generation"] PUPPETEER["Puppeteer
Headless Browser"] end WAF --> PRE PRE --> MJS MJS --> WWEB MJS --> QRCODE MJS --> PUPPETEER WAF --> PYO PYO --> APP APP --> EXC
Diagram sources
Section sources
Electron main process manages the WhatsApp client lifecycle, Puppeteer configuration, event emission, and cleanup.
Preload script exposes a secure IPC API to the renderer for WhatsApp operations.
React component handles UI rendering, QR display, status updates, and user actions.
Pyodide runtime enables Python-powered manual number parsing directly in the browser.
Python backend provides robust contact extraction and validation utilities.
Section sources
The integration follows an event-driven model:
Renderer triggers client initialization via IPC
Main process creates a Client with LocalAuth and headless Puppeteer
QR event emits a data URL for the renderer to display
Ready and authenticated events clear QR and update status
Disconnected and auth_failure events notify the renderer
Message sending loops through contacts with personalization and rate limiting
Contact import supports CSV and TXT formats with fallback parsing
Diagram sources
Client Initialization Pattern with LocalAuth and Puppeteer#
Client creation uses LocalAuth strategy for automatic session persistence and improved security.
Puppeteer runs in headless mode with hardened arguments to improve stability on CI and desktop environments.
Event handlers are attached immediately after client instantiation to capture qr, ready, authenticated, auth_failure, and disconnected states.
Initialization is triggered via IPC from the renderer and guarded against duplicate instances.
Diagram sources
Section sources
Event-Driven Architecture#
qr: Converts QR string to a data URL and sends it to the renderer; updates status to prompt scanning.
ready: Clears QR display and signals readiness.
authenticated: Clears QR display and confirms successful authentication.
auth_failure: Emits a failure message with the provided reason.
disconnected: Emits disconnection reason and resets the client reference.
Diagram sources
Section sources
QR Code Generation and Display Mechanism#
QR string received from the client is transformed into a data URL using the QRCode library.
The renderer displays the QR code image and handles load/error states.
On ready or authenticated events, the QR is cleared to prevent stale displays.
Diagram sources
Section sources
Message Sending Workflow: Personalization, Rate Limiting, Error Handling#
Personalization: Replaces {{name}} with the contact’s name or defaults to “Friend”.
Chat ID construction: Ensures proper format (@c.us) for both numbered and international formats.
Rate limiting: Delays between messages using timeouts to reduce spam risk.
Error handling: Per-contact try/catch captures registration checks and send failures; updates status and continues.
Diagram sources
Section sources
Contact Import Functionality: CSV and TXT#
CSV import: Uses a streaming parser to read rows and extract number/name pairs.
TXT import: Reads file content and splits lines; attempts to parse comma/tab/pipe-separated values or extract phone numbers from lines.
Fallback parsing: Attempts pandas-based parsing first, then falls back to manual CSV parsing if needed.
Error handling: Returns empty array on errors and logs failures.
Diagram sources
Section sources
Authentication Lifecycle Management, Session Persistence, and Cleanup#
Session persistence: LocalAuth stores session data locally, enabling seamless reconnects.
Startup cleanup: Removes cached .wwebjs_cache and .wwebjs_auth directories to ensure a fresh start.
Logout procedure: Calls client.logout(), clears client reference, deletes auth/cache files, and resets UI state.
Forced cleanup: Even if logout fails, auth/cache files are removed and UI state is reset.
Diagram sources
Section sources
Key external dependencies and their roles:
whatsapp-web.js: Provides the WhatsApp Web client and LocalAuth strategy.
qrcode: Converts QR strings to data URLs for display.
qrcode-terminal: Alternative terminal-based QR generation (present in dependencies).
nodemailer/googleapis: Used for email features (not covered in this document).
Puppeteer: Headless browser engine configured in the Electron main process.
Diagram sources
Section sources
Headless browser configuration: Hardened Chromium flags improve stability and reduce resource contention.
Rate limiting: Delays between messages help avoid rate limits and detection mechanisms.
Streaming parsers: CSV parsing uses streaming to handle large files efficiently.
Cleanup: Removing auth/cache directories prevents accumulation of stale session data.
[No sources needed since this section provides general guidance]
Common issues and resolutions:
QR code not loading: Verify network connectivity, restart the app, and clear browser cache. The renderer includes retry logic on QR load failure.
Authentication failures: Check device link instructions, ensure phone has active internet, and retry. The client emits auth_failure with a reason.
Disconnections: The client emits disconnected with a reason; restart the client to reconnect.
Contact import errors: Confirm file format compatibility (CSV/TXT), UTF-8 encoding, and proper column headers. The backend includes fallback parsing strategies.
Logout issues: The logout handler attempts logout and forces cleanup if it fails, ensuring auth/cache files are removed.
Section sources
The integration leverages a robust event-driven architecture with LocalAuth for session persistence, a headless Puppeteer configuration for reliability, and comprehensive error handling and cleanup procedures. The message sending workflow incorporates personalization and rate limiting, while contact import supports flexible formats with fallback parsing. Together, these components deliver a resilient and user-friendly WhatsApp Web integration suitable for bulk messaging scenarios.